/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TP2;

/**
 *
 * @author augusto
 */

import java.text.NumberFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.data.statistics.HistogramType;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Histograma extends ApplicationFrame {

    public Histograma(String title,int intervalos,double vector[]) {
        super(title);
        JPanel chartPanel = crearPanel(intervalos,vector);
        chartPanel.setPreferredSize(new java.awt.Dimension(700, 475));
        chartPanel.setDoubleBuffered(true);
        setContentPane(chartPanel);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    
    private static IntervalXYDataset crearDataset(double vector[],int intervalos) {
        HistogramDataset dataset = new HistogramDataset();
       //vector de numeros totalmente aleatorios
       
        double vector2[]=new double[vector.length/intervalos];
        for (int i = 0; i < vector2.length; i++) {
            vector2[i]=(i/vector2.length);
          }
        dataset.addSeries(" Frecuencia Observada ", vector, intervalos);
      //  dataset.addSeries(" Frecuencia Esperada ",vector2,1,0,1);
        dataset.setType(HistogramType.FREQUENCY);
       
       
        return dataset;
    }

    private static JFreeChart crearChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createHistogram(
                "Histograma",
                null,
                null,
                dataset,
                PlotOrientation.VERTICAL,
                true,
                true,
                false);
        XYPlot plot = (XYPlot) chart.getPlot();
       
        chart.getXYPlot().setForegroundAlpha(0.75f);   
        
       // XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
       // renderer.setDrawBarOutline(true);
   //     try {
   //         ChartUtilities.saveChartAsJPEG(new File("/home/Desktop/histograma.jpg"), chart, 500, 475);
    //    } catch (IOException e) {
    //        System.out.println("Error al abrir el archivo");
     //   }
        return chart;
    }

    private  ChartPanel createChart (double vector[],int intervalos)  { 
        XYDataset roiData = crearDataset(vector, intervalos); 
        JFreeChart chart = ChartFactory.createTimeSeriesChart("" ,  "Value" ,  "Value" , roiData ,  true ,  true ,  false ); 
        XYPlot plot = chart.getXYPlot(); 
        XYLineAndShapeRenderer renderer=(XYLineAndShapeRenderer)plot.getRenderer(); 
        renderer . setBaseShapesVisible ( true ); 
        NumberFormat currency =  NumberFormat . getCurrencyInstance (); 
        currency . setMaximumFractionDigits ( 0 ); 
        NumberAxis rangeAxis =  ( NumberAxis ) plot . getRangeAxis (); 
        rangeAxis . setNumberFormatOverride ( currency );
        return  new  ChartPanel ( chart ); 
    }
    
    public static JPanel crearPanel(int intervalos,double vector[]) {
        JFreeChart chart = crearChart(crearDataset(vector,intervalos));
        
        ChartPanel panel=new ChartPanel(chart);
        return panel;
    }

    public static void generarHistograma(String titulo,int intervalos, double vector[]) 
    {
        Histograma histo = new Histograma(titulo,intervalos,vector);
        
        histo.pack();
        RefineryUtilities.centerFrameOnScreen(histo);
        
        histo.setVisible(true);
       // Histograma histo2=new Histograma(titulo,intervalos,vector);
       // histo2.add(histo.createChart(vector, intervalos), -1);
        
        
    }

    

}

